package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Banner;
import com.itheima.domain.BannerExample;

import com.itheima.mapper.BannerMapper;
import com.itheima.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Admin
 */
@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    BannerMapper bannerMapper;

    @Override
    public PageInfo<Banner> findByPage(Integer pageNum, Integer pageSize,String keyword) {
        PageHelper.startPage(pageNum,pageSize);

       BannerExample bannerExample = new BannerExample();
        BannerExample.Criteria criteria = bannerExample.createCriteria();
    // 进行搜索查询前提必须有example中的 criteria1 进行部分的字体查询
        if (keyword != null && !"".equals(keyword)){
            criteria.andTitleLike("%"+keyword+"%");
        }
//   下来直接查询所有；
        List<Banner> banners = bannerMapper.selectByExample(bannerExample);
        // 接下来把获取的对象进行封装成对象
        PageInfo<Banner> pageInfo = new PageInfo(banners);
        return pageInfo;
    }

    /**
     * 前台查询分页
     * @return
     */
    @Override
    public List<Banner> findBannerList() {
        PageHelper.startPage(1,5);

        BannerExample bannerExample = new BannerExample();

        // 时间查询降序 desc  , 升序asc   这个得写判断的上边下边基于上边的按sql语句
        bannerExample.setOrderByClause("id asc");

        List<Banner> banners = bannerMapper.selectByExample(bannerExample);
        return banners;
    }

    @Override
    public void save(Banner banner) {

        // 因为这里添加和修改时同一张表所以要进行判断
        if (banner.getId() == null){
//
            bannerMapper.insertSelective(banner);
        }else {
            bannerMapper.updateByPrimaryKeySelective(banner);
        }
    }

    /**
     * 回显示数据
     * @param id
     * @return
     */
    @Override
    public Banner findById(Integer id) {
        return  bannerMapper.selectByPrimaryKey(id);

    }

    @Override
    public void delete(Integer id) {
        bannerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteBanTch(Integer[] ids) {
        for (Integer id : ids) {
            bannerMapper.deleteByPrimaryKey(id);
        }
    }
}
